import pygame
import game_engine.GameEngine

class logger:

    def info(self,txt):
        print txt

MODE_TEXT = 0
MODE_ART = 1
MODE_DEBUG = 2
RIGHT = 0
LEFT = 1
        

class UIManager:

    def __init__(self, resourceManager):
        print "UIManager::Init"
        self.m_mode = MODE_TEXT
        self.m_font = pygame.font.Font(None, 36)
        self.m_consoleFontSize = 12 
        self.m_itemFontSize = 12
        self.m_consoleFont = pygame.font.Font(None,self.m_consoleFontSize)
        self.m_itemFont = pygame.font.Font(None,self.m_itemFontSize)
        self.m_consoleNumberOfCharactersAllowed =0
        self.m_textArray = []
        self.m_maxConsoleLines = 10
        self.m_entityManager = game_engine.GameEngine.getGameEngine().getEntityManager()
        self.m_resourceManager = game_engine.GameEngine.getGameEngine().getResourceManager()


        
    #Set Debug
    def setDebug(self,debug):
        self.m_debug = debug

    #Set out logger
    def setLogger(self,logger):
        self.logger = logger


    def addTextToConsole(self,txt):
        self.m_textArray.append(txt)
        
        
    #our draw method
    def draw(self,surface,x,y):
        #Dray UI background
        
        #This was an attempt to have non rectangular panes for these displays
        #leftUIPanePointList = []
        #leftUIPanePointList.append((-y/2,0))
        #leftUIPanePointList.append((-y/2,-x/2))
        #leftUIPanePointList.append((0,-x/2))
        #leftUIPanePointList.append((0,0))
        
        leftPane = pygame.Surface((200, 100))
        leftPane.fill((0,0,0))
        leftPane.set_alpha(200)
        
        
        rightPane = pygame.Surface((200, 100))
        rightPane.fill((0,0,0))
        rightPane.set_alpha(200)

       
        #Our inventory pane
        centerPane = pygame.Surface((300,50))
        centerPane.fill((0,0,0))
        centerPane.set_alpha(200)

        
        # get values to update
        gameEngine = game_engine.GameEngine.getGameEngine()
        self.m_player = self.m_entityManager.getPlayerEntity()


        #we need to figure out how many characters we can fit in the panel
        self.m_consoleNumberOfCharactersAllowed = (rightPane.get_width() / self.m_consoleFontSize)
        i=0
        self.m_textArray.reverse()
        for txt in self.m_textArray[:10]:
            fitTxt = txt
            rText = self.m_consoleFont.render(str(fitTxt),1,(255,255,255))
            txtHeight = self.m_consoleFont.get_height()
            i += txtHeight
            rightPane.blit(rText,(0,i))
        self.m_textArray.reverse()
                    

      
        #Draw our inventory
        if not (self.m_player == None):
            if not (self.m_player.getFullInventory() == None):
                i=10
                slotNbr = 1
                for item in self.m_player.getFullInventory():
                    if(item == None):
                        continue
                    imageFileName = item.getImageFileName()
                    itemSurface = self.m_resourceManager.loadImage(imageFileName,(20,20))
                    txtSurface = self.m_itemFont.render(str(slotNbr),1,(255,255,255))


                    border = pygame.Surface((itemSurface.get_width() + 2,itemSurface.get_height()+2))
                    border.fill((10,10,10))
                    centerPane.blit(border,(i,10))
                    centerPane.blit(txtSurface,(i,1))
                    centerPane.blit(itemSurface,(i,10))
                    i+=itemSurface.get_width()+10
                    slotNbr += 1




        

        # player data
        health = gameEngine.getEntityManager().getPlayerEntity().getCurrentHealth()
        
        # level data
        # use this section to get level data (displayed to the bottom right of the screen.)
        
        
        #handle player state display
        text = self.m_font.render(str(health), 1, (255,255,255))
        textpos = text.get_rect()
        textpos.x = 0
        leftPane.blit(text, textpos)
        
        surfaceRect = surface.get_rect()
        surface.blit(leftPane, (0, surfaceRect.h - leftPane.get_height()))
        
        # handle level state display
        surface.blit(rightPane, (surfaceRect.w-200, surfaceRect.h-rightPane.get_height()))

        #Blit our center surface
        #find the center of our screen
        x = surfaceRect.w / 2
        x -= centerPane.get_width() /2
        surface.blit(centerPane,(x, surfaceRect.h-centerPane.get_height()))
        
        
